<!doctype html>
<html>

	<head>
		<meta charset="UTF-8">
		<title></title>
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<link href="../css/mui.min.css" rel="stylesheet" />
		<link rel="stylesheet" type="text/css" href="../css/beiyong.css"/>
		<style type="text/css">
			.ic{
				height: 100%;
				display: flex;
				align-items: center;
				font-family: Muiicons;
			    font-size: 38px;
			    line-height: 1;
			    z-index: 20;
			    padding-top: 10px;
			    padding-bottom: 10px;
			    color: #FFFFFF;
			}
			.kong{
				width: 100%;
				text-align: center;
				margin-top:40%;
			}
			.kong>div{
				margin: 20px;
			}
			.content{
				width: 100%;
				/*padding: 10px;*/
			}
			.list{
				width: 100%;
				/*border-bottom: 1px solid #fff;*/
				padding: 0.5em;
				background:#fff;
				margin-bottom:5px;
			}
			.list_top{ border-bottom:1px dashed #EBF3FE;}
			.list_bottom{ display:flex; justify-content:space-between; padding-top:0.5em;}
			.list_bottom  button{ padding:2px 12px;}
			.list_bottom input[type="radio"]{top:2px!important; left:5px!important;}
			.list_bottom input[type="radio"]:before{ font-size:20px; line-height: 20px; }
			.list_bottom input[type='radio']:checked:before {
				content: '\e442';
				font-size: 20px;;
				color:#e4393c!important;
			}
			.list_bottom label{padding:0 0 0 30px!important; height:20px; font-size:14px;  }
			.list_top>div{
				padding: 3px;
			}
			*{
				font-family: "微软雅黑";
				color: #676664;
			}
			.text{
				font-size: 0.8em;
				width:100%; overflow:hidden;  text-overflow:ellipsis; display:-webkit-box;  -webkit-box-orient: vertical;
				-webkit-line-clamp:2;
			}
			.ml10{
				margin-left: 10px;
			}
		</style>
	</head>

	<body>
		<header class="mui-bar mui-bar-nav hbg">
		    <a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>
		    <h1 class="mui-title">地址管理</h1>
		    <div class="ic mui-icon-right-nav mui-pull-right" onclick="goadd()">+</div>
		</header>
		
		<div class="mui-content" id="app">
			<div class="content" id="app_content" >
			
			</div>
		</div>
		
		<script src="../js/jquery.min.js" type="text/javascript" charset="utf-8"></script>
		<script src="../js/vue.js" type="text/javascript" charset="utf-8"></script>
		<script src="../js/mui.min.js"></script>
		<script src="../js/muishow.js" type="text/javascript" charset="utf-8"></script>
		<script src="../js/req.js" type="text/javascript" charset="utf-8"></script>
		<script type="text/javascript">
			window.onload=function(){
				mui.init()
			
				
				/*$("input:radio[name='radio']").on('change', function(event){
					       alert($(this).val());
				});*/
				mui.plusReady(function(){
					var old_blac = mui.back;
					mui.back = function(e){
							var list = plus.webview.getWebviewById("home_details");
							mui.fire(list,'refresh',{});
							old_blac();
					}
				})
				
				setlist();
					
			}
			
			window.addEventListener('rest',function(e){
				setlist();
			})
			var ist;
			

			mui.plusReady(function(){
					let list = plus.webview.currentWebview();
					ist = list.ist || 0;
					
			})
			function setlist(){
				let data = {
					url:"/api/Address/address_list",
					data:{access_token : acctoken()||""}
				};
				ajax(data,function(res){
					console.log(res);
					if(res.code == -1){
						mui.toast(res.message);
					}
					if(res.code == 1){
						let data = res.data,
						str = "";
						let app = document.getElementById("app_content");
						if(data.length > 0){
							for (let i = 0; i < data.length; i++) {
								str += '<div class="list" ><div class="list_top"><div class="">' +
								data[i].name + '  ' + data[i].mobile + 
								'</div><div class="text">' +
								data[i].province +' ' + data[i].city + ' ' + data[i].county + ' ' + data[i].address + 
								'</div></div><div class="list_bottom "><div class="mui-input-row mui-radio mui-left"><label>设为默认地址</label>';
								if(data[i].status == 1){
									str += '<input name="radio" type="radio" checked onchange="setmoren('+ data[i].id +')">' ;
								}else{
									str += '<input name="radio" type="radio"  onchange="setmoren('+ data[i].id +')">' ;
								}
								str +='</div><div>'+
								'<button  onclick="bianji('+ data[i].id +')">编辑</button>'+
								'<button class="ml10" onclick="deladdress('+ data[i].id +')">删除</button></div></div></div>'
							}
						}else{
							str = "<div class='kong'><div class=''>"+
							"暂无地址，请添加"+
							"</div> <button type='button' class='mui-btn mui-btn-blue' onclick='goadd()'>新增地址</button></div>"
						}
						app.innerHTML = str;
					}
				})
			}
			function setmoren(id){
				let data = {
					url:"/api/Address/setdefault",
					data:{access_token : acctoken()||"",id:id}
					};
				ajax(data,function(res){
					console.log(res);
					if(res.code == -1){
						mui.toast(res.message);
					}
					if(res.code == 1){
						mui.toast("设置成功");
						setlist();
						console.log(ist,"ist");
						if(ist){
							let ist = {ist:1}
							mui.back();
//							mui.openWindow({
//							    url: '../home/home_details.html', 
//							    id:'home_details',
//							    extras:ist
//							 });
						}
					}
				})
			}
			function bianji(id){
				mui.openWindow({
				    url: 'addadd.html?id='+id, 
				    id:'addadd',
				    extras: {  
        				id:id
   					}  
				 });
			}
			function deladdress(id){
				mui.confirm('是否删除默认地址？','',['取消','确认'],function (e) {
					if(e.index == 1){
						let data = {
							url:"/api/Address/del",
							data:{access_token : acctoken()||"",id:id}
						};
						ajax(data,function(res){
							console.log(res);
							if(res.code == -1){
								mui.toast(res.message);
							}
							if(res.code == 1){
								mui.toast("删除成功");
								
								setlist();
							}
						})
					}
				})
			}
			function goadd(){
				mui.openWindow({
				    url: 'addadd.html', 
				    id:'addadd'
				 });
			}
			
		</script>
	</body>

</html>